var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var config=require('../config');
var OrderRecord=require('./orderRecord')
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    console.log(error);
});

db.model('orderRecord',OrderRecord.orderRecordSchema);
var orderReplySchema=new Schema({
    replyMessage:String,
    replyDate:{type:Date,default:Date.now},
    replyOrder:{type:Schema.Types.ObjectId,ref:'orderRecord'}
});
//users---对应数据库表名
var OrderReply=db.model('OrderReply',orderReplySchema);

/**
 * 获得简介信息
 */

OrderReply.prototype.findAllOrderReply=function(callback){
    OrderReply.find({},function(err,datas){
        callback(err,datas);
    })
}

OrderReply.prototype.updateOrderReplyById=function(id,options,callback){
    OrderReply.update({_id:id},options,function(err){
        callback(err)
    });
}
OrderReply.prototype.findOrderReplyById=function(id,callback){
    OrderReply.findOne({_id:id},function(err,data){
        callback(err,data);
    });
}
OrderReply.prototype.findOrderReplyByOptions=function(options,callback){
    OrderReply.find(options,function(err,datas){
        callback(err,datas);
    });
}
OrderReply.prototype.delOrderReplyById=function(id,callback){
    OrderReply.remove({_id:id},function(err){
        callback(err);
    })
}

OrderReply.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=OrderReply.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit).sort('orderDate').populate('orderCalendar orderToUser orderUser');
    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            OrderReply.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null, count,pageCount, datas);
                }
            });
        }
    });

}
module.exports =OrderReply;


